package com.example.springboottest.util;

/*
 *1.Result<T> 自定义泛型类
 * 2.Result<T>对象就是后端返回前端的数据,以json格式返回
 */
public class Result<T> {
    private String code;
    private String msg;
    private T data;

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    //返回表示成功的Result对象
    public static Result success(){
        Result result = new Result<>();
        result.setCode("200");
        result.setMsg("success");
        return result;
    }
    //返回表示成功并且携带数据的Result对象
    //如果需要在static方法使用泛型,需要在static <T>
    public static <T> Result<T> success(T data){
        Result<T> result = new Result<>(data);
        result.setCode("200");
        result.setMsg("success");
        return result;
    }
    //返回表示失败的Result对象
    public static Result error(String code,String msg){
        Result result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
    //返回表示成功并且携带数据的Result对象
    //如果需要在static方法使用泛型,需要在static <T>
    public static <T> Result<T> error(String code,String msg,T data){
        Result<T> result = new Result<>(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
